package cn.xspace.library.util;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xspace.android.xafcommon.appliction.BaseApplication;
import com.xspace.android.xafcommon.been.AppIndexInfo;
import com.xspace.android.xafcommon.been.AppInfoBean;
import com.xspace.android.xafcommon.been.NavTagBean;
import com.xspace.android.xafcommon.been.UserInfoBean;
import com.xspace.android.xafcommon.util.Constants;
import com.xspace.android.xafcommon.util.PreferencesUtils;
import com.xspace.android.xafcommon.util.XmlUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import cn.xspace.library.bean.BuildBean;
import cn.xspace.library.bean.GuidePageBean;
import cn.xspace.library.bean.NavigationBean;

/**
 * Created by liuchen on 2016/11/30.
 */

public class BuildCache {
    public static BuildBean mBuildConfig;

    public static BuildBean getAppConfigBean(Context context) {

        if (mBuildConfig == null) {
            mBuildConfig = new BuildBean();
            try {
                mBuildConfig.appInfo = parseBuildConfigBean(context.getAssets().open("app_config.xml"));
                mBuildConfig.appIndexInfo = new AppIndexInfo();
                mBuildConfig.userInfoBean = new UserInfoBean();
                mBuildConfig.navigationBean = new NavigationBean();
                mBuildConfig.guidePageBean = new GuidePageBean();
                mBuildConfig.appInfo.app_deptid = (String) PreferencesUtils.getPfValue(BaseApplication.mInstance,
                        Constants.ACCOUNT_APP_ORGID,
                        String.class.getName());
//                NavTagBean navTagBean = new NavTagBean();
//                navTagBean.navKey = BuildConfig.MODULE_KEY_WNX;
//                navTagBean.navName = BuildConfig.MODULE_NAME_WNX;
//                NavTagBean navTagBean2 = new NavTagBean();
//                navTagBean2.navKey = BuildConfig.MODULE_KEY_SETTING;
//                navTagBean2.navName = BuildConfig.MODULE_NAME_SETTING;
//                mBuildConfig.navigationBean.navTagList.add(navTagBean);
//                mBuildConfig.navigationBean.navTagList.add(navTagBean2);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                InputStream is = null;
                byte[] bytes = new byte[1024];
                int length;

                File file = context.getFileStreamPath(Constants.NAVIGATION_JSON);
                if (file.exists()) {
                    is = context.openFileInput(Constants.NAVIGATION_JSON);
                } else {
                    is = context.getAssets().open(Constants.NAVIGATION_JSON);
                }
                while ((length = is.read(bytes)) != -1) {
                    baos.write(bytes, 0, length);
                }
                List<NavTagBean> tagList = new Gson().<ArrayList<NavTagBean>>fromJson(new String(baos.toByteArray(),
                                "UTF-8"),
                        new TypeToken<ArrayList<NavTagBean>>() {
                        }.getType());
                mBuildConfig.navigationBean.navTagList.clear();
                mBuildConfig.navigationBean.navTagList.addAll(tagList);
                setNavigationIcon();
            } catch (Exception e) {
                mBuildConfig = new BuildBean();
            }
        }
        return mBuildConfig;
    }

//    public static AppInfoBean appInfoBean;
//    public static AppInfoBean getAppConfigBean(Context context) {
//
//        if (BuildCache.appInfoBean == null) {
//            try {
//                BuildCache.appInfoBean = BuildBean
//                        .parseBuildConfigBean(context.getAssets().open(
//                                "build_config.xml"));
//            } catch (Exception e) {
//                BuildCache.mBuildConfig = new BuildBean();
//            }
//        }
//        return BuildCache.mBuildConfig;
//    }

    public static AppInfoBean parseBuildConfigBean(InputStream inStream) {
        AppInfoBean bean = XmlUtils.toBean(AppInfoBean.class, inStream);
        return bean;
    }

    public static void setNavigationIcon() {
        for (NavTagBean navTagBean : mBuildConfig.navigationBean.navTagList) {
            if (BuildConfig.nativeMap.containsKey(navTagBean.navKey)) {
                navTagBean.iconName = BuildConfig.nativeMap.get(navTagBean.navKey).getResId();
            }
        }
    }

}
